package com.abigdreamer.wordpress.newspider;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**   
 * 
 * @author Darkness
 * @date 2013-6-2 下午04:26:48
 * @website http://www.abigdreamer.com 
 * @version V1.0   
 */
public class UrlExtractor {
	/**
	 * 找出当前页面中所有匹配的url地址
	 * html示例：
	 * String html = "
	 * <body>
	 * 	<a href="thread-1-1-1.html" class="xst">a href</a>
	 * </body>";
	 * UrlExtractor.getHtmlURL(html, "thread-(\\d+)-(\\d+)-(\\d+).html", "[class=xst]", "http://www.abc.com/");
	 * host 需要已“/”结尾
	 * @param pageStr  网页字符串
	 * @return { thread-1-1-1.html }
	 */
	public static Map<String, String> getHtmlURL(String pageContext, String urlPattern, String clazz, String host){
		if(pageContext == null){
			return null;
		}
		Map<String, String> articleURLS = new LinkedHashMap<String, String>();
		//System.out.println("寻找子url...........");
		try {
			Document doc = Jsoup.parseBodyFragment(pageContext);
			Elements es = doc.select("a" + clazz);//"div[class=fl bm]"); 
//			es = es.select("a");
			int i = 0;
			//System.out.println(es.size());
			for(Element e:es){
//				if(e.attr("href").indexOf("thread-") > -1) {
//					System.out.println("article:" + e.text());
//				}
//				System.out.println((++i) + "" + e.attr("href").indexOf("thread-") + "" + e);
				articleURLS.put(e.attr("href"), e.text());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		Map<String, String> result = new LinkedHashMap<String, String>();
		Pattern pattern = Pattern.compile(urlPattern);
		for (String url : articleURLS.keySet()) {
			Matcher matcher = pattern.matcher(url);
			if(matcher.find()) {
				String fixUrl = url;
				if(!fixUrl.startsWith("http")) {
					fixUrl = host + fixUrl;
				}
				result.put(fixUrl, articleURLS.get(url));
			}
		}
		return result;
	}
}
